<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

use App\Models\VersionCdnManager;
use App\Models\Area;
use App\Models\EMail;

use Encore\Admin\Grid;
use Encore\Admin\Form;
use App\Admin\Extra\Search\Html;
use \Illuminate\Support\Facades\Input;

class VersionController extends Controller
{
    public function anyManagerCdnList() {
        return Admin::content(function (Content $content) {
            $content->header('Manager CDN used list.');
            $append_button[] = 	Html::button('Android', \App\Service\Common::build_url_query(url()->current(), ['m_type'=>'android']), 'btn btn-goto btn-success');
            $append_button[] = 	Html::button('IOS', \App\Service\Common::build_url_query(url()->current(),  ['m_type'=>'ios']), 'btn btn-goto btn-danger');
            $content->row(Html::append($append_button));
            $grid = Admin::grid(VersionCdnManager::class, function(Grid $grid) use($use_data){
                $grid->model()->where('deleted_at', 0);

                $grid->actions(function($actions)use(&$row){
                    $row = $actions->row;
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $url_edit = \App\Service\Common::build_url_query('/admin/version/manager-edit', [
                        'm_type' =>  $row->m_type,
                        'version_code'    =>  $row->version_code,
                        'version_name'    =>  $row->version_name,
                        'value'  =>  $row->value,
                        'upgrade' => $row->upgrade,
                        'guest' => $row->guest,
                        'areas' => $row->areas,
                    ]);
                    $actions->append(Html::button('CLONE', \App\Service\Common::build_url_query($url_edit, ['clone'=>1]), 'btn-goto btn-xs'). ' ');
                    $actions->append(Html::button('EDIT', $url_edit, 'btn-goto btn-xs').' ');
                    $actions->append(Html::button('DEL', \App\Service\Common::build_url_query('/admin/version/api-manager-save', [
                        'm_type' =>  $row->m_type,
                        'version_code'    =>  $row->version_code,
                        'version_name'    =>  $row->version_name,
                        'value'  =>  -1,
                    ]), 'confirm-ajax btn-danger btn-xs btn-clear'));
                });

			    // 第一列显示id字段，并将这一列设置为可排序列
			    $grid->m_type('MobileModel');
                $grid->version_code('AppVersionCode');
                $grid->version_name('AppVersionName');
                $grid->value('value')->display(function($value){
                    $select = VersionCdnManager::getValueSelect();
                    return $select[$value];
                });
                $grid->upgrade('Upgrade')->display(function($value){
                    $select = VersionCdnManager::getValueUpgradeSelect();
                    return $select[$value];
                });
                $grid->guest('Guest')->display(function($value){
                    $value = intval($value);
                    $select = VersionCdnManager::getValueOpenSelect();
                    return $select[$value];
                });
                
			    $grid->filter(function ($filter) use($use_data) {

			    });
                $grid->disableCreateButton();
                $grid->tools(function (Grid\Tools $batch) {
                
                    $batch->append('<a href="/admin/version/manager-edit" class="btn btn-sm btn-success"><i class="fa fa-trash"></i>&nbsp;&nbsp;new</a>');
                });
            });
        
            $content->row($grid->render());
        
        });
    }

    public function anyManagerEdit(){
        $req = Input::all();
        $data['one'] = [
            'm_type' => $req['m_type'],
            'version_code'    =>  $req['version_code'],
            'version_name'    =>  $req['version_name'],
            'value' => $req['value'],
            'upgrade' => $req['upgrade'],
            'guest' => $req['guest'],
            'areas' => $req['areas'],
        ];
        if ($req['clone']) {
            $data['one']['version_code'] = 0;
        }
        \Log::info("[MANAGER_EDIT][PARAM]", $req);
        return Admin::content(function($content) use($data){
		    $content->header('邮件编辑');
            $grid = Admin::Form(VersionCdnManager::class, function(Form $form)use($data){
                $one = $data['one'];
                //$one->area_list = explode(',', $one->area_list);

                $form->text('m_type', 'MobileModel')->default($one['m_type']);
                $form->text('version_code', 'VersionCode')->default($one['version_code']);
                $form->text('version_name', 'VersionName')->default($one['version_name']);
                $form->select('value', 'Value')->options(VersionCdnManager::getValueSelect())->default($one['value']);
                $form->select('upgrade', 'Upgrade')->options(VersionCdnManager::getValueUpgradeSelect())->default($one['upgrade']);
                $form->select('guest', 'guest')->options(VersionCdnManager::getValueOpenSelect())->default($one['guest']);
                $form->listbox('areas', 'areas')->options(\App\Models\Area::areaSelectData(0))->default($one['areas']);
     
                //保存后回调
                $form->saved(function (Form $form) {
                    \Log::info("[MANAGER_EDIT][AFTER]");
                    return redirect('/admin/version/manager-list');
                });
                
                
                $form->setAction(\App\Service\Common::build_url_query('/admin/version/api-manager-save', [
                ]));

                $form->method('POST');
                // $form->disablePjax();

            });
            $content->row($grid);
        });


    }

    // 版本管理
    public function anyApiManagerUpgrade() {
        $req = Input::all();
       
        $ret = VersionCdnManager::upgradeManagerCdn($req['m_type'], $req['version_code'], $req['version_name'], $req['value'], $req['upgrade']);
        echo json_encode(['code' => $ret,'status'=>true, 'msg' => admin_toastr('upgrade verison ok.','success'), 'data' => null]);
        //return redirect('/admin/version/manager-list?m_type='.$req['m_type']);
    }

    // 游戏页签 列表管理页面
    public function anyLabelList() {
        return Admin::content(function (Content $content) {
            $use_data['area'] = Input::get('area');
            $area_info = \App\Models\Area::areaInfo($use_data['area']);
            $content->header('Manager Game Label. Server:' . $area_info['name']. '['.$area_info['areaId'].']');
            
            
            $grid = Admin::grid(\App\Models\GameLabel::class, function(Grid $grid) use($use_data){
                $grid->model()->where('deleted_at', 0);

                $grid->actions(function($actions)use(&$row, $use_data){
                    $row = $actions->row;
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->append(Html::button('Open', \App\Service\Common::build_url_query('/admin/version/label-save', [
                        'area' =>  $use_data['area'],
                        'field'    =>  $row->key,
                        'state'    =>  1,
                    ]), 'btn-goto-ajax btn-xs'));
                    $actions->append(Html::button('Close', \App\Service\Common::build_url_query('/admin/version/label-save', [
                        'area' =>  $use_data['area'],
                        'field'    =>  $row->key,
                        'state'    =>  0,
                    ]), 'btn-goto-ajax btn-danger btn-xs btn-clear'));
                });

			    // 第一列显示id字段，并将这一列设置为可排序列
			    $grid->key('Key');
                $grid->alias('Alias');
                
                $grid->state('State')->display(function($value){
                    $value = intval($value);
                    $select = VersionCdnManager::getValueOpenSelect();
                    return $select[$value];
                });
                
			    $grid->filter(function ($filter) use($use_data) {

			    });
                $grid->disableCreateButton();
                $grid->tools(function (Grid\Tools $batch) {
                
                    $batch->append('<a href="/admin/version/manager-edit" class="btn btn-sm btn-success"><i class="fa fa-trash"></i>&nbsp;&nbsp;new</a>');
                });
            });
        
            $content->row($grid->render());
        
        });       
    }

    public function anyLabelSave(){
        $area = Input::get('area');
        $fld = Input::get('field');
        $state = Input::get('state');
        \App\Models\GameLabel::saveLabel($area, [$fld => $state]);
        echo json_encode(['code' => $ret,'status'=>true, 'msg' => admin_toastr('upgrade game label ok.','success'), 'data' => null]);
    }
}